<template>
        <a-progress  v-show="exporting" :percent="exportPercent" status="active" />
</template>
<script>
import {downFile} from '../../../api/manage'
export default {
    name:"AkFileDown",
    describe:"文件下载",

    props:{
        url:{
            type:String,
            required:true,
        },
        fileName:{
            type:String,
            required:false,
        }
    },
    data(){
        return {
            exporting:false,
            exportPercent:0,
        }
    },
    methods:{
        down(){
            this.exporting = true,
            this.exportPercent = 0;
            downFile(this.url, null, (event)=>{
                console.log(event)
                if(event.total){
                    
                    this.exportPercent = parseFloat((event.loaded/event.total*100).toFixed(1));
                }
            }).then(res=>{
                this.exporting=false;
                this.exportPercent=100;
                let filename = '';
                let contentDisposition = res.headers['content-disposition'];
                if(contentDisposition){
                    let index = contentDisposition.indexOf('filename=');
                    if(index>-10){
                        filename = contentDisposition.substr(index+9);
                    }
                }else{
                    let index = 0;
                    while(index<this.url.length && this.url.indexOf("/", index+1) > -1 ){
                        index = this.url.indexOf("/", index+1) + 1;
                    }
                    filename = this.url.substr(index);
                }
                if(this.fileName){
                    filename = this.fileName;
                }
                let a = document.createElement('a');
                a.href = URL.createObjectURL(res.data);
                a.download = decodeURIComponent(filename);
               a.click();
            }).catch(e=>{
                this.exporting=false;
                this.exportPercent=0;
                console.warn(e);
                 let a = document.createElement('a');
                a.href = this.url;
                let names = this.url.split("/");
                a.download = decodeURIComponent(names[names.length-1]);
               a.click();
            });
        }
    }
}
</script>